package com.yz.fanrenmianshi.controller;

import com.yz.fanrenmianshi.common.BaseResponse;
import com.yz.fanrenmianshi.common.ResultUtils;
import com.yz.fanrenmianshi.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public BaseResponse<String> upload(@RequestParam("file") MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            log.info("上传文件开始:{}", originalFilename);
            String url = aliOssUtil.upload(file.getBytes(), originalFilename);
            return ResultUtils.success(url);
        } catch (Exception e) {
            log.error("上传文件失败:{}", e.getMessage());
        }
        return ResultUtils.error(50000, "上传失败");
    }
}
